<%@ page language="java" contentType="text/javascript; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>

<%@page import="javapns.communication.ConnectionToAppleServer"%>
<%@page import="javapns.devices.implementations.basic.BasicDevice"%>
<%@page import="javapns.notification.AppleNotificationServerBasicImpl"%>
<%@page import="javapns.notification.PushNotificationManager"%>
<%@page import="javapns.notification.PushNotificationPayload"%>
<%@page import="javapns.notification.PushedNotification"%>
<%@page import="javapns.notification.ResponsePacket"%>

<%@page import="java.util.LinkedList"%>
<%@page import="java.util.ListIterator"%>
<%@page import="javapns.devices.Device"%>
<%@page import="javapns.feedback.AppleFeedbackServerBasicImpl"%>
<%@page import="javapns.feedback.FeedbackServiceManager"%>
<%@page import="javapns.notification.ResponsePacket"%>


<%String callback = request.getParameter("callback");%>
<%=callback %> (<%
	String iphoneID = request.getParameter("regid");
	
	String host = "gateway.sandbox.push.apple.com";
	int port = 2195;
	int badge = 1;
	
	String certi = "www/Triends/Triends.p12";
	String passwd = "SUDadventurer2";
	
	PushNotificationPayload payload = new PushNotificationPayload();
	payload.addAlert("안녕");
	payload.addBadge(-1);
	payload.addSound("default");
	payload.addCustomDictionary("sign", "PushMagic");
	
	BasicDevice device = new BasicDevice(iphoneID);
	device.setDeviceId("iphone");
	JSONObject jo = new JSONObject();
	
	
	PushNotificationManager push = new PushNotificationManager();
	
	AppleNotificationServerBasicImpl appleserver = new AppleNotificationServerBasicImpl(certi, passwd, ConnectionToAppleServer.KEYSTORE_TYPE_PKCS12, host, port);
	push.initializeConnection(appleserver);
		
	PushedNotification pushed = push.sendNotification(device, payload);
	
	ResponsePacket resultPacket = pushed.getResponse();
	
	if (resultPacket != null) {
		out.println(resultPacket.isErrorResponsePacket());
		out.println(resultPacket.isValidErrorMessage());
		out.println(resultPacket.getStatus());
		out.println(resultPacket.getMessage());
	}
	
	push.stopConnection();
	
	
	FeedbackServiceManager feedback = new FeedbackServiceManager();
	String feedbackway = "feedback.sandbox.push.apple.com";
	int feedbackport = 2196;
	
	AppleFeedbackServerBasicImpl feedbackserver = new AppleFeedbackServerBasicImpl(certi, passwd, ConnectionToAppleServer.KEYSTORE_TYPE_PKCS12, feedbackway, feedbackport);
	
	LinkedList<Device> devices = feedback.getDevices(feedbackserver);
	
	ListIterator<Device> itr = devices.listIterator();
	
	out.println(itr);
	
%>);